package com.cat.chapter046;

import java.util.HashMap;

/**
 * @author cat
 * @description https://leetcode.cn/problems/longest-well-performing-interval/
 * @create 2025/7/15 16:02
 * @since JDK17
 */

public class Solution04 {
    static HashMap<Integer, Integer> map = new HashMap<>();
    int ans, sum;

    public int longestWPI(int[] hours) {
        map.clear();
        map.put(0, -1);
        for (int i = 0; i < hours.length; i++) {
            sum += hours[i] > 8 ? 1 : -1;
            ans = Math.max(ans, i - map.getOrDefault(sum - 1, Integer.MAX_VALUE));
            if (!map.containsKey(sum)) {
                map.put(sum, i);
            }
        }
        return ans;
    }
}
